// pages/addPerson/addPerson.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    employee_mobile: "", // 手机号码
    card_no: "", // 身份证号
    vcode: "", //验证码
    company_id: 0, //企业ID（这里没办法取参数，只能先写死）
    employee_name: "", //姓名
    sex: "1", // 性别
    code: "",
    genderList: [{
        title: "男",
        value: 1,
        checked: true,
        index: 0
      },
      {
        title: "女",
        value: 2,
        checked: false,
        index: 1
      }
    ],
    is_certification: 0
  },

  /**
   * 员工信息修改
   * @param {*} e 
   */
  onChange: function (e) {
    this.setData({
      employee_mobile: e.detail
    });
  },

  // 性别
  genderChange(val) {
    let {
      index,
      checked,
      value
    } = val.currentTarget.dataset.item || {}, list = this.data.genderList || [];
    for (let i = 0; i < list.length; i++) {
      list[i]['checked'] = false;
    }
    list[index]['checked'] ? list[index]['checked'] = false : list[index]['checked'] = true;
    this.setData({
      genderList: list
    })
  },

  /**
   * 获取验证码
   */
  getVerification: function () {
    if (!this.data.employee_mobile) {
      return wx.$Toast('请输入手机号码');
    }
    let num = 120;
    this.setData({
      code: num + 's'
    })
    let t = setInterval(() => {
      num -= 1;
      if (num >= 0) {
        this.setData({
          code: num + 's'
        })
      } else {
        this.setData({
          code: null
        })
        clearInterval(t)
      }
    }, 1000)
    wx.$api.sendSms({
      mobile: this.data.employee_mobile,
      type: 'bind'
    }).then(res => {});

  },
  /**
   * 提交员工信息
   * @param {*} options 
   */
  submitPersonInfo: function (e) {
    let sendParams = e.detail.value;
    sendParams["company_id"] = this.data.company_id;
    if (this.data.is_certification != 1 && !sendParams.employee_name) {
      return wx.$Toast('请输入姓名');
    }
    if (this.data.is_certification != 1 && !sendParams.card_no) {
      return wx.$Toast('请输入身份证号');
    }

    if (this.data.is_certification != 1 && !wx.$c.isCard(sendParams.card_no)) {
      return wx.$Toast('请输入正确的身份证号');
    }
    if (this.data.is_certification != 1 && !sendParams.employee_mobile) {
      return wx.$Toast('请输入手机号码');
    }
    if (this.data.is_certification != 1 && !wx.$c.isMobile(sendParams.employee_mobile)) {
      return wx.$Toast('请输入正确的手机号码');
    }
    if (this.data.is_certification != 1 && !sendParams.vcode) {
      return wx.$Toast('请输入验证码');
    }
    /*
    if (!this.data.company_id) {
      return wx.$Toast('企业ID出错');
    }
    */
    // let post = {
    //   employee_mobile: this.data.employee_mobile,
    //   vcode: this.data.vcode,
    //   card_no: this.data.card_no,
    //   employee_name: this.data.employee_name,
    //   company_id: this.data.company_id
    // };
    // console.log('post', post);
    wx.$api.bindCompany(sendParams).then(res => {
      if (res.data.code != 200) {
        if (this.data.is_certification == 1) {
          return wx.navigateTo({
            url: '/pages/index/index',
          });
        }
        wx.$Toast(res.data.msg);
        return;
      }
      wx.$Toast(res.data.msg);
      wx.navigateTo({
        url: '/pages/index/index',
      });
      wx.setStorageSync('company_id', this.data.company_id);
      wx.setStorageSync('status', 0);
    });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, '::::::::::::options', wx.getStorageSync('is_certification'))
    let options_company_id = options.scene || 0; //通过扫邀请揽件员二维码进来
    let status = wx.getStorageSync('status');
    let company_id = wx.getStorageSync('company_id');
    let is_certification = wx.getStorageSync('is_certification')
    //设置当前页面绑定的企业ID
    this.setData({
      company_id: options_company_id,
      is_certification: is_certification
    });
    if (options_company_id && status == 1) {
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }
    if (company_id && status == 0) {
      wx.navigateTo({
        url: '/pages/verify/verify',
      })
    }

    if (is_certification == 1) {
      this.submitPersonInfo({
        detail: {
          value: {}
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})